<template>
  <a-dropdown v-if="user" placement="bottomRight">
    <span class="ic-account-avatar">
      <a-avatar size="small" src="https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png" class="antd-pro-global-header-index-avatar" />
      <span>{{ user.realName }}</span>
    </span>
    <template #overlay>
      <a-menu class="ant-pro-drop-down menu" :selected-keys="[]">
        <a-menu-item key="center" @click="handleToCenter">
          <a-icon type="user" />
          {{ $t('header.userMenu.dropdown.center') }}
        </a-menu-item>
        <a-menu-item key="settings" @click="handleToSettings">
          <a-icon type="setting" />
          {{ $t('header.userMenu.dropdown.settings') }}
        </a-menu-item>
        <a-menu-divider />
        <a-menu-item key="logout" @click="handleLogout">
          <a-icon type="logout" />
          {{ $t('header.userMenu.dropdown.logout') }}
        </a-menu-item>
      </a-menu>
    </template>
  </a-dropdown>
  <span v-else>
    <a-spin size="small" :style="{ marginLeft: 8, marginRight: 8 }" />
  </span>
</template>
<script lang="ts">
import { State } from 'vuex-class'
import { Vue, Component } from 'vue-property-decorator'

import { UserState } from '@/types/user.state'

@Component({
  name: 'AvatarDropdown'
})
export default class AvatarDropdown extends Vue {
  @State('user')
  user!: UserState

  handleToCenter () {
    this.$router.push({ path: '/account/center' })
  }

  handleToSettings () {
    this.$router.push({ path: '/account/settings' })
  }

  handleLogout () {
    this.$confirm({
      title: this.$t('header.userMenu.dialog.title'),
      content: this.$t('header.userMenu.dialog.content'),
      onOk: () => {
        this.$message.success('注销成功')
      }
    })
  }
}
</script>
<style lang="less" scoped>
.ant-pro-drop-down {
  /deep/ .action {
    margin-right: 8px;
  }
  /deep/ .ant-dropdown-menu-item {
    min-width: 160px;
  }
}
</style>
